(function() {
    angular.module('app').component('brandStatDataComponent', {
        templateUrl: 'app/components/brand.stat.data.component/brand.stat.data.component.html',
        controller: brandStatDataComponent,
        controllerAs: 'vm',
        bindings: {
            brandName: '<'
        }
    });

    brandStatDataComponent.$inject = ['util', 'brandStatDataComponentService','$rootScope'];

    function brandStatDataComponent(util, brandStatDataComponentService,$rootScope) {
        var vm = this;

        //vm.bardName

        vm.brandInfo = {};

        //各类目销售额占比
        vm.optionCat = {
            title: {
                text: '各类目销售额占比',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                // formatter: "{a} <br/>{b} : {c} ({d}%)"
                formatter: function(params) {
                    // console.log(params)
                    var str = '<div style = "text-align: center;">' + params.seriesName + '</div>';

                    str += '<div style = "text-align: left;">' + params.name + ' : ' + util.bigNumFormat(params.value) + ' (' + params.percent + '%)</div>';

                    return str;
                }
            },
            legend: {
                orient: 'vertical',
                left: 'right',
                data: []
            },
            series: [{
                name: '销售额',
                type: 'pie',
                radius: '55%',
                center: ['50%', '55%'],
                data: [],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }]
        };

        //平台销售额对比
        vm.option = {
            title: {
                text: '平台销售额对比',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                // formatter: "{a} <br/>{b} : {c} ({d}%)"
                formatter: function(params) {
                    // console.log(params)
                    var str = '<div style = "text-align: center;">' + params.seriesName + '</div>';

                    str += '<div style = "text-align: left;">' + params.name + ' : ' + util.bigNumFormat(params.value) + ' (' + params.percent + '%)</div>';

                    return str;
                }
            },
            series: [{
                name: '销售额',
                type: 'pie',
                radius: '55%',
                center: ['50%', '55%'],
                data: [],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }]
        };

        // if($(window).width()<=1920){
        //     vm.optionCat.series[0].radius = '45%';
        //     vm.option.series[0].radius = '45%';
        // }
        
        init();

        function init() {   

            if($(window).width()<=992 && $(window).width()>600){
                $('.brandStatBox table.responsive-table').removeClass('responsive-table');
            }

            // console.log('brandStatDataComponent:vm.brandName' +vm.brandName);

            $rootScope.$emit('echart_draw_line_loading_EV', 'brandStatDataPie');
            brandStatDataComponentService.getBrandInfo(vm.brandName).then(function(data) {
                // vm.shopInfo = data;

                vm.brandInfo = data;


                var option = angular.copy(vm.option);

                option.series[0].data = [{
                    name: '天猫',
                    value: vm.brandInfo.tSellMoney
                }, {
                    name: '淘宝',
                    value: vm.brandInfo.cSellMoney
                }, {
                    name: '天猫国际',
                    value: vm.brandInfo.thkSellMoney
                }, {
                    name: '全球购',
                    value: vm.brandInfo.gSellMoney
                }];

                $rootScope.$emit('echart_draw_line_EV', option, 'brandStatDataPie');
            }, function(err) {

            });


            $rootScope.$emit('echart_draw_line_loading_EV', 'brandStatDataPieCat');
            //获取拼图
            brandStatDataComponentService.getCatBrand(vm.brandName).then(function(data) {
                var optionCat = angular.copy(vm.optionCat);


                //data.catDetail;
                for (var i in data.catDetail) {
                    optionCat.series[0].data.push({
                        name: i,
                        value: data.catDetail[i].sellMoney
                    });
                }


                $rootScope.$emit('echart_draw_line_EV', optionCat, 'brandStatDataPieCat');

            }, function(err) {

            });
        }


    }
})();
